/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cl.orientesalud.gui.operaciones;

import cl.orientesalud.business.TratamientoDelegate;
import cl.orientesalud.dominio.Descuento;
import cl.orientesalud.dominio.Medico;
import cl.orientesalud.dominio.Paciente;
import cl.orientesalud.dominio.TipoTratamiento;
import cl.orientesalud.dominio.Tratamiento;
import cl.orientesalud.gui.mantenedores.MantenedorPaciente;
import cl.orientesalud.util.OrienteLog;
import cl.orientesalud.util.UtilForm;
import java.awt.Frame;
import java.util.List;
import javax.swing.DefaultListModel;
import javax.swing.JOptionPane;
import org.apache.log4j.Logger;

/**
 *
 * @author nicolas
 */
public class IngresarTratamiento extends javax.swing.JDialog {
    Logger logger=OrienteLog.obtenerLogger(IngresarTratamiento.class);
    Frame parent;
    UtilForm utilForm;
    TratamientoDelegate delegate;
    DefaultListModel modeloDescSel;       
    DefaultListModel modeloDesc; 
    Long valorTratamiento=new Long(0);
    Paciente pacienteObtenido;
    /**
     * Creates new form IngresarTratamiento
     */
    public IngresarTratamiento(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        this.parent=parent;
        utilForm=new UtilForm();
        delegate=new TratamientoDelegate();
        initComponents();
        this.setLocation(300, 100);
        modeloDescSel=new DefaultListModel();
        modeloDesc=new DefaultListModel();
        lstDescSel.setModel(modeloDescSel);
        lstDescuentos.setModel(modeloDesc);
        dtpFechaInicio.setDate(null);
        dtpFechaFin.setDate(null);
        
        
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        txtRut = new javax.swing.JTextField();
        btnBuscar = new javax.swing.JButton();
        lblNombre = new javax.swing.JTextField();
        lblAppat = new javax.swing.JTextField();
        lblApmat = new javax.swing.JTextField();
        lblDireccion = new javax.swing.JTextField();
        lblTelefono = new javax.swing.JTextField();
        jPanel2 = new javax.swing.JPanel();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        jLabel10 = new javax.swing.JLabel();
        dtpFechaInicio = new com.toedter.calendar.JDateChooser();
        dtpFechaFin = new com.toedter.calendar.JDateChooser();
        cmbTipoTratamiento = new javax.swing.JComboBox();
        txtValor = new javax.swing.JTextField();
        jLabel11 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        lstDescSel = new javax.swing.JList();
        jLabel12 = new javax.swing.JLabel();
        jLabel13 = new javax.swing.JLabel();
        jLabel14 = new javax.swing.JLabel();
        lblTotalConsulta = new javax.swing.JLabel();
        lblTotalMedico = new javax.swing.JLabel();
        btnAgregarDescuento = new javax.swing.JButton();
        btnQuitarDescuento = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        lstDescuentos = new javax.swing.JList();
        jLabel15 = new javax.swing.JLabel();
        cmbMedico = new javax.swing.JComboBox();
        jLabel16 = new javax.swing.JLabel();
        txtPie = new javax.swing.JTextField();
        txtCuotas = new javax.swing.JTextField();
        jLabel17 = new javax.swing.JLabel();
        jLabel18 = new javax.swing.JLabel();
        txtOtroDescuento = new javax.swing.JTextField();
        lblMensaje = new javax.swing.JLabel();
        btnCancelar = new javax.swing.JButton();
        btnLimpiar = new javax.swing.JButton();
        btnGuardar = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Ingresar Tratamiento");
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowOpened(java.awt.event.WindowEvent evt) {
                formWindowOpened(evt);
            }
        });

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Datos Paciente"));

        jLabel1.setText("Rut:");

        jLabel2.setText("Nombre:");

        jLabel3.setText("Apellido Paterno:");

        jLabel4.setText("Apellido Materno:");

        jLabel5.setText("Dirección:");

        jLabel6.setText("Teléfono:");

        btnBuscar.setText("Buscar");
        btnBuscar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnBuscarActionPerformed(evt);
            }
        });

        lblNombre.setEnabled(false);

        lblAppat.setEnabled(false);

        lblApmat.setEnabled(false);

        lblDireccion.setEnabled(false);

        lblTelefono.setEnabled(false);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addComponent(jLabel2)
                    .addComponent(jLabel5))
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGap(3, 3, 3)
                                .addComponent(txtRut, javax.swing.GroupLayout.PREFERRED_SIZE, 89, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(btnBuscar))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(lblNombre, javax.swing.GroupLayout.PREFERRED_SIZE, 161, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jLabel3)))
                        .addGap(3, 3, 3)
                        .addComponent(lblAppat, javax.swing.GroupLayout.PREFERRED_SIZE, 132, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(3, 3, 3)
                        .addComponent(lblDireccion)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel4)
                    .addComponent(jLabel6))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(lblTelefono, javax.swing.GroupLayout.DEFAULT_SIZE, 218, Short.MAX_VALUE)
                    .addComponent(lblApmat))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(txtRut, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnBuscar))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(lblNombre, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblAppat)
                    .addComponent(lblApmat, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel5)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(lblDireccion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel6)
                        .addComponent(lblTelefono, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Datos Tratamiento"));

        jLabel7.setText("Fecha inicio:");

        jLabel8.setText("Fecha término:");

        jLabel9.setText("Tipo tratamiento:");

        jLabel10.setText("Valor:");

        cmbTipoTratamiento.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmbTipoTratamientoActionPerformed(evt);
            }
        });

        txtValor.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                txtValorFocusLost(evt);
            }
        });

        jLabel11.setText("Descuentos:");

        jScrollPane1.setViewportView(lstDescSel);

        jLabel12.setText("Descuentos seleccionados:");

        jLabel13.setText("Total a pagar consulta:");

        jLabel14.setText("Total a pagar médico:");

        lblTotalConsulta.setText("0");

        lblTotalMedico.setText("0");

        btnAgregarDescuento.setText(">>");
        btnAgregarDescuento.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAgregarDescuentoActionPerformed(evt);
            }
        });

        btnQuitarDescuento.setText("<<");
        btnQuitarDescuento.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnQuitarDescuentoActionPerformed(evt);
            }
        });

        jScrollPane2.setViewportView(lstDescuentos);

        jLabel15.setText("Médico:");

        jLabel16.setText("Valor Pie:");

        txtPie.setText("0");
        txtPie.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtPieActionPerformed(evt);
            }
        });

        txtCuotas.setText("0");
        txtCuotas.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtCuotasActionPerformed(evt);
            }
        });

        jLabel17.setText("Número de cuotas:");

        jLabel18.setText("Otro Descuento:");

        txtOtroDescuento.setText("0");
        txtOtroDescuento.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                txtOtroDescuentoFocusLost(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel7)
                    .addComponent(jLabel9)
                    .addComponent(jLabel11)
                    .addComponent(jLabel15)
                    .addComponent(jLabel18))
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGap(12, 12, 12)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(jPanel2Layout.createSequentialGroup()
                                        .addComponent(cmbTipoTratamiento, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addGap(191, 191, 191))
                                    .addGroup(jPanel2Layout.createSequentialGroup()
                                        .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 340, Short.MAX_VALUE)
                                        .addGap(18, 18, 18)
                                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addComponent(btnAgregarDescuento)
                                            .addComponent(btnQuitarDescuento))
                                        .addGap(28, 28, 28)))
                                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(jLabel10)
                                    .addComponent(jLabel12)
                                    .addComponent(jLabel13)
                                    .addComponent(jLabel14)))
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addComponent(dtpFechaInicio, javax.swing.GroupLayout.PREFERRED_SIZE, 178, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(54, 54, 54)
                                .addComponent(jLabel8)
                                .addGap(0, 0, Short.MAX_VALUE)))
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addComponent(dtpFechaFin, javax.swing.GroupLayout.DEFAULT_SIZE, 149, Short.MAX_VALUE)
                                .addComponent(txtValor)
                                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE))
                            .addComponent(lblTotalConsulta, javax.swing.GroupLayout.PREFERRED_SIZE, 106, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(lblTotalMedico, javax.swing.GroupLayout.PREFERRED_SIZE, 106, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(50, 50, 50))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGap(24, 24, 24)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(cmbMedico, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(txtOtroDescuento, javax.swing.GroupLayout.PREFERRED_SIZE, 106, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel17)
                    .addComponent(jLabel16))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(txtPie, javax.swing.GroupLayout.PREFERRED_SIZE, 106, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtCuotas, javax.swing.GroupLayout.PREFERRED_SIZE, 106, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel7)
                    .addComponent(dtpFechaInicio, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel8)
                    .addComponent(dtpFechaFin, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel9)
                        .addComponent(cmbTipoTratamiento, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jLabel10)
                    .addComponent(txtValor, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGap(18, 18, 18)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel11)
                                    .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                        .addComponent(jLabel12)
                                        .addComponent(btnAgregarDescuento)))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(btnQuitarDescuento))
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 82, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGap(10, 10, 10)
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel13)
                            .addComponent(lblTotalConsulta))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel14)
                            .addComponent(lblTotalMedico))
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 29, Short.MAX_VALUE)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel18)
                            .addComponent(txtOtroDescuento, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(cmbMedico, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel15))))
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(txtPie, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel16))
                .addGap(18, 18, 18)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(txtCuotas, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel17))
                .addGap(20, 20, 20))
        );

        btnCancelar.setText("Salir");
        btnCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelarActionPerformed(evt);
            }
        });

        btnLimpiar.setText("Limpiar");
        btnLimpiar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnLimpiarActionPerformed(evt);
            }
        });

        btnGuardar.setText("Guardar");
        btnGuardar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnGuardarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(lblMensaje, javax.swing.GroupLayout.PREFERRED_SIZE, 416, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(btnGuardar)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnLimpiar)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(btnCancelar)
                .addGap(29, 29, 29))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnGuardar)
                    .addComponent(btnLimpiar)
                    .addComponent(btnCancelar))
                .addGap(5, 5, 5)
                .addComponent(lblMensaje, javax.swing.GroupLayout.DEFAULT_SIZE, 19, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnBuscarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnBuscarActionPerformed
        String mensaje = "";
        
        if(!utilForm.validarFormaRut(txtRut.getText())){
           mensaje = "El rut debe ser de la forma [numero]-[digito verificador]";
            JOptionPane.showMessageDialog(this.parent,
                                    mensaje,
                                    "Problemas de validación",
                                    JOptionPane.WARNING_MESSAGE);
           
        }
        else{
            if(!utilForm.validarRut(txtRut.getText())){
                mensaje="Rut no válido";
                 JOptionPane.showMessageDialog(this.parent,
                                    mensaje,
                                    "Problemas de validación",
                                    JOptionPane.WARNING_MESSAGE);
            }
            else{
                Paciente paciente=new Paciente();
                paciente.setRut(txtRut.getText());
                try {
                    paciente= delegate.obtenerPaciente(paciente);
                    if(paciente!=null){
                        txtRut.setText(paciente.getRut());
                        lblNombre.setText(paciente.getNombre());
                        lblAppat.setText(paciente.getApellidoPaterno());
                        lblApmat.setText(paciente.getApellidoMaterno());
                        lblDireccion.setText(paciente.getDireccion());
                        lblTelefono.setText(paciente.getTelefono());
                        txtRut.setEnabled(false);
                        pacienteObtenido=paciente;
                    }
                    else{
                        mensaje = "El paciente no existe. Debe ingresarlo";
                         JOptionPane.showMessageDialog(this.parent,
                                    mensaje,
                                    "Problemas de validación",
                                    JOptionPane.WARNING_MESSAGE);
                        MantenedorPaciente mantenedorPaciente=new MantenedorPaciente(this.parent,true);
                        mantenedorPaciente.setVisible(true);
                        pacienteObtenido=null;
                    }
                } catch (Exception ex) {
                    logger.error("Error:"+ex.getMessage());
                    lblMensaje.setText(ex.getMessage());
                }
            }
        }
    }//GEN-LAST:event_btnBuscarActionPerformed

    private void btnCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelarActionPerformed
        this.dispose();
    }//GEN-LAST:event_btnCancelarActionPerformed

    private void btnLimpiarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnLimpiarActionPerformed
        utilForm.limpiarFormulario(this.jPanel1);
        utilForm.limpiarFormulario(this.jPanel2);
        modeloDescSel.removeAllElements();
        TipoTratamiento temp=(TipoTratamiento) cmbTipoTratamiento.getSelectedItem();
        txtValor.setText(String.valueOf(temp.getValor()));
        valorTratamiento=temp.getValor();
        lblTotalConsulta.setText("0");
        lblTotalMedico.setText("0");
        txtRut.setEnabled(true);
        lblMensaje.setText("");
    }//GEN-LAST:event_btnLimpiarActionPerformed

    private void formWindowOpened(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowOpened
        delegate=new TratamientoDelegate();
        try {
            List<TipoTratamiento> tipos=delegate.obtenerTipoTratamientos();
            utilForm.llenarCombo(cmbTipoTratamiento, tipos);
            TipoTratamiento tipoTratamiento=(TipoTratamiento) cmbTipoTratamiento.getSelectedItem();
            List<Descuento> descuentos=delegate.obtenerDescuentos(tipoTratamiento);
            utilForm.llenarLista(lstDescuentos, descuentos);
            List<Medico> medicos=delegate.obtenerMedicos();
            utilForm.llenarCombo(cmbMedico, medicos);
        } catch (Exception ex) {
            logger.error("Error:"+ex.getMessage());
            lblMensaje.setText(ex.getMessage());
        }
    }//GEN-LAST:event_formWindowOpened

    private void cmbTipoTratamientoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmbTipoTratamientoActionPerformed
        try {
            TipoTratamiento tipoTratamiento=(TipoTratamiento) cmbTipoTratamiento.getSelectedItem();
            List<Descuento> descuentos=delegate.obtenerDescuentos(tipoTratamiento);
            modeloDesc.removeAllElements();
            utilForm.llenarLista(lstDescuentos, descuentos);
            txtValor.setText(String.valueOf(tipoTratamiento.getValor()));
            valorTratamiento=tipoTratamiento.getValor();
            Long tempRepartir=valorTratamiento/2;
            lblTotalConsulta.setText(String.valueOf(tempRepartir));
            lblTotalMedico.setText(String.valueOf(tempRepartir));
            modeloDescSel.removeAllElements();
        } catch (Exception ex) {
            logger.error("Error:"+ex.getMessage());
            lblMensaje.setText(ex.getMessage());
        }
    }//GEN-LAST:event_cmbTipoTratamientoActionPerformed

    private void btnAgregarDescuentoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAgregarDescuentoActionPerformed
      
       Descuento descuentoSel=(Descuento) lstDescuentos.getSelectedValue(); 
       
       if(descuentoSel!=null){
            Long valorDesc=descuentoSel.getValor();
            Long totalConsulta=Long.valueOf(lblTotalConsulta.getText().equals("")?"0":lblTotalConsulta.getText());
            Long totalMedico=Long.valueOf(lblTotalMedico.getText().equals("")?"0":lblTotalMedico.getText());
            Long tempTotal;
            Long tempRepartir;

            if(!modeloDescSel.contains(descuentoSel)){
                 modeloDescSel.addElement(descuentoSel);
                 tempTotal=Long.valueOf(txtValor.getText());
                 valorTratamiento-=valorDesc;
                 tempRepartir=valorTratamiento/2;
                 lblTotalConsulta.setText(String.valueOf(tempRepartir));
                 lblTotalMedico.setText(String.valueOf(tempRepartir));
            }
       }
       
       
       
    }//GEN-LAST:event_btnAgregarDescuentoActionPerformed

    private void btnQuitarDescuentoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnQuitarDescuentoActionPerformed
        Descuento descuentoSel=(Descuento) lstDescSel.getSelectedValue();
         Long tempTotal;
         Long tempRepartir;
        if(descuentoSel!=null){           
            Long valorDesc=descuentoSel.getValor();
            tempTotal=Long.valueOf(txtValor.getText());            
            valorTratamiento+=valorDesc;
            tempRepartir=valorTratamiento/2;
            lblTotalConsulta.setText(String.valueOf(tempRepartir));
            lblTotalMedico.setText(String.valueOf(tempRepartir));
            modeloDescSel.removeElement(descuentoSel);
        }
    }//GEN-LAST:event_btnQuitarDescuentoActionPerformed

    private void btnGuardarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnGuardarActionPerformed
            
            if(!validarFormulario()){
                    Tratamiento tratamiento=new Tratamiento();
                    tratamiento.setDescuentos(utilForm.obtenerLista(modeloDescSel));
                    tratamiento.setFechaInicio(dtpFechaInicio.getDate());
                    tratamiento.setFechaTermino(dtpFechaFin.getDate());
                    tratamiento.setMedico((Medico)cmbMedico.getSelectedItem());
                    tratamiento.setPaciente(pacienteObtenido);
                    tratamiento.setTipo((TipoTratamiento)cmbTipoTratamiento.getSelectedItem());
                    tratamiento.setValorRepartir(valorTratamiento);
                    tratamiento.setCuotas(Long.valueOf(txtCuotas.getText()));
                    tratamiento.setPie(Long.valueOf(txtPie.getText()));
                    tratamiento.setOtroDescuento(Long.valueOf(txtOtroDescuento.getText()));
                try {
                    Integer id=delegate.insertarTratamiento(tratamiento);
                    lblMensaje.setText("Se ha ingresado correctamente el tratamiento numero "+id);
                } catch (Exception ex) {
                    logger.error("Error:"+ex.getMessage());
                    lblMensaje.setText(ex.getMessage());
                }
            }
            
            
    }//GEN-LAST:event_btnGuardarActionPerformed

    private void txtCuotasActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtCuotasActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtCuotasActionPerformed

    private void txtValorFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txtValorFocusLost
        Long tempRepartir;
        valorTratamiento=Long.valueOf(txtValor.getText());
        modeloDescSel.removeAllElements();
        tempRepartir=valorTratamiento/2;
        lblTotalConsulta.setText(String.valueOf(tempRepartir));
        lblTotalMedico.setText(String.valueOf(tempRepartir));
    }//GEN-LAST:event_txtValorFocusLost

    private void txtPieActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtPieActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtPieActionPerformed

    private void txtOtroDescuentoFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txtOtroDescuentoFocusLost
        Long tempRepartir;
        valorTratamiento-=Long.valueOf(txtOtroDescuento.getText());
        tempRepartir=valorTratamiento/2;
        lblTotalConsulta.setText(String.valueOf(tempRepartir));
        lblTotalMedico.setText(String.valueOf(tempRepartir));
    }//GEN-LAST:event_txtOtroDescuentoFocusLost

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(IngresarTratamiento.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(IngresarTratamiento.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(IngresarTratamiento.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(IngresarTratamiento.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the dialog */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                IngresarTratamiento dialog = new IngresarTratamiento(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAgregarDescuento;
    private javax.swing.JButton btnBuscar;
    private javax.swing.JButton btnCancelar;
    private javax.swing.JButton btnGuardar;
    private javax.swing.JButton btnLimpiar;
    private javax.swing.JButton btnQuitarDescuento;
    private javax.swing.JComboBox cmbMedico;
    private javax.swing.JComboBox cmbTipoTratamiento;
    private com.toedter.calendar.JDateChooser dtpFechaFin;
    private com.toedter.calendar.JDateChooser dtpFechaInicio;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel14;
    private javax.swing.JLabel jLabel15;
    private javax.swing.JLabel jLabel16;
    private javax.swing.JLabel jLabel17;
    private javax.swing.JLabel jLabel18;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTextField lblApmat;
    private javax.swing.JTextField lblAppat;
    private javax.swing.JTextField lblDireccion;
    private javax.swing.JLabel lblMensaje;
    private javax.swing.JTextField lblNombre;
    private javax.swing.JTextField lblTelefono;
    private javax.swing.JLabel lblTotalConsulta;
    private javax.swing.JLabel lblTotalMedico;
    private javax.swing.JList lstDescSel;
    private javax.swing.JList lstDescuentos;
    private javax.swing.JTextField txtCuotas;
    private javax.swing.JTextField txtOtroDescuento;
    private javax.swing.JTextField txtPie;
    private javax.swing.JTextField txtRut;
    private javax.swing.JTextField txtValor;
    // End of variables declaration//GEN-END:variables

    private boolean validarFormulario() {
        boolean retorno;
        String mensaje = "";
        retorno=false;
        Long totalDescuentos=0l;
        if(pacienteObtenido==null){
            mensaje+=System.getProperty("line.separator")+"Debe seleccionar un paciente";
            retorno=true;
        }
        if(dtpFechaInicio.getDate()==null || dtpFechaFin.getDate()==null){
            mensaje+=System.getProperty("line.separator")+"Debe ingresar fechas de inicio y fin de tratamiento";
            retorno=true;
        }
        if(txtValor.getText().equals("")){
            mensaje+=System.getProperty("line.separator")+"El valor no puede estar vacío";
            retorno=true;
        }else{
            for(int i=0;i<modeloDescSel.size();i++){
                Descuento desc=(Descuento) modeloDescSel.get(i);
                totalDescuentos+=desc.getValor();
            }
            /*Long valorPie=Long.valueOf(txtPie.getText());
            Long valorOtroDescuento=Long.valueOf(txtOtroDescuento.getText());
            if(valorPie<totalDescuentos+valorOtroDescuento){
                mensaje+=System.getProperty("line.separator")+"El valor del pie debe ser mayor o igual que la suma de los descuentos";
                retorno=true;
            }*/
        }
        if(txtPie.getText().equals("0")){
             mensaje+=System.getProperty("line.separator")+"El pie no puede ser 0";
            retorno=true;
        }
        
        if(modeloDescSel.isEmpty()){
            mensaje+=System.getProperty("line.separator")+"Debe seleccionar al menos un descuento";
            retorno=true;
        }
        
        if(retorno)
             JOptionPane.showMessageDialog(this.parent,
                                    mensaje,
                                    "Problemas de validación",
                                    JOptionPane.WARNING_MESSAGE);
        return retorno;
    }
}
